package cn.com.hhrcw.service.impl;

import cn.com.hhrcw.service.*;
import cn.com.hhrcw.vo.SyncDataVO;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SyncServiceImpl implements SyncService {

    private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALGBILhOuNuVmDfyfA9LwE0mxz15c+eUxsTEYGa3C3IE8WAj03t3RY+TsmFQ3Z/3zSn5q9pVnYz5S+00Ahr8stOZ++wVQlno0TZJF/RgoGl8nzwC/n9NM8dskka23GyZ5mXkmiu6sZYvL1H8LpB2UOd3SenFJJ6X9wnDchtQ8pOlAgMBAAECgYALQJbM6DyiuuR343jG1XP+0A8AUCMvUoiZL6O2zzK8CyEsCYleAc0YkXHU5nXuLpzEZO3mfNfseF8+TbY2dL93PHxZe3Ae4fumb6KaB7qAkuTDb3KGI/5D0sxtoop/mOg6FRGnj5/hvoriv9aNGp6tK5dCYSUIBNyqVTdiiKoTjwJBANV0Vultg/PDoPgT7KVKIn8Yq6TQMintNo5HRtY37rFhe0xN/20CyUVWhq/2xhj7zdiTyCardSmtB594QlcXqhMCQQDU4mmWlOKlAbn2doKFHAcgsJlpvLMWP7Thmtzw3wWxkKfXoNSxiptYSuVxDFI52lXWRCLjXq+dYtwovPOfowJnAkAwHM0KSHkZlNrPkA4jI5aq9zCZMaRc9u75Gu/IkUroCIT2Kg6Mz7f2PQ6W0xTLxNYdHxJaRQNfpXS2aTDq2JapAkEAtuLj7xdtNnCrQA9DxfL0NKNCyNHx0JVeYcrXMJ+Ilqk9aEOlNflrx4hgYKRDJ6xovbC/gDHiEQXTObRKX8R81QJBAKktZ1y3zlz5G8kGGJa4LVX9a/M04++VtGGIQ8Cf0o8hIV4sQc0bYL69lidYUM4igJF9C7zd81nfI4UfCfGIC24=";
    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxgSC4TrjblZg38nwPS8BNJsc9eXPnlMbExGBmtwtyBPFgI9N7d0WPk7JhUN2f980p+avaVZ2M+UvtNAIa/LLTmfvsFUJZ6NE2SRf0YKBpfJ88Av5/TTPHbJJGttxsmeZl5JorurGWLy9R/C6QdlDnd0npxSSel/cJw3IbUPKTpQIDAQAB";

    private static final Logger logger = LoggerFactory.getLogger(SyncServiceImpl.class);
    @Autowired
    private IPiInfoService piInfoService;
    @Autowired
    private ICvCertificateService certificateService;
    @Autowired
    private ICvFamilyMemberService familyMemberService;
    @Autowired
    private ICvLanguageLevelService languageLevelService;
    @Autowired
    private ICvProjectInfoService projectInfoService;
    @Autowired
    private ICvResumeService resumeService;
    @Autowired
    private ICvSkillsService skillsService;
    @Autowired
    private ICvTrainService trainService;
    @Autowired
    private ICvUserEduInfoService eduInfoService;
    @Autowired
    private ICvUserWorkInfoService workInfoService;
    @Autowired
    private ICvWorkIntentionService workIntentionService;

    @Override
    public String getSyncData(String syncDate) throws Exception{
        SyncDataVO syncDataVO = new SyncDataVO();
        Date beginTime = DateUtils.parseDate(syncDate+" 00:00:00","yyyyMMdd HH:mm:ss");
        Date endTime = DateUtils.parseDate(syncDate+" 23:59:59","yyyyMMdd HH:mm:ss");

        syncDataVO.setPiInfoList(piInfoService.getByDate(beginTime,endTime));
        syncDataVO.setCertificateList(certificateService.getByDate(beginTime,endTime));
        syncDataVO.setFamilyMemberList(familyMemberService.getByDate(beginTime,endTime));
        syncDataVO.setLanguageLevelList(languageLevelService.getByDate(beginTime,endTime));
        syncDataVO.setProjectInfoList(projectInfoService.getByDate(beginTime,endTime));
        syncDataVO.setResumeList(resumeService.getByDate(beginTime,endTime));
        syncDataVO.setSkillsList(skillsService.getByDate(beginTime,endTime));
        syncDataVO.setTrainList(trainService.getByDate(beginTime,endTime));
        syncDataVO.setEduInfoList(eduInfoService.getByDate(beginTime,endTime));
        syncDataVO.setWorkInfoList(workInfoService.getByDate(beginTime,endTime));
        syncDataVO.setWorkIntentionList(workIntentionService.getByDate(beginTime,endTime));


        String json = JSON.toJSONString(syncDataVO);
        RSA enRSA = new RSA(null,PUBLIC_KEY);
        return HexUtil.encodeHexStr(enRSA.encrypt(StrUtil.bytes(json, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey));

    }
}
